package com.gome.boot.config.app;

import com.gome.boot.common.diamond.DiamondBean;
import com.gome.boot.common.diamond.MultiDiamondUtils;
import com.gome.boot.common.logger.BaseLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.StandardEnvironment;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * Spring基础应用配置.
 *
 * @author baoxiufeng
 */
public abstract class BaseAppConfig extends BaseLogger {

    @Autowired
    protected StandardEnvironment env;

    /**
     * 启动自动从diamond里加载properties放到环境变量中，所有启动加载的Diamond配置内容都可以从中取到属性值.
     */
    @PostConstruct
    public void loadDiamond() {
        System.out.println();
        logger.info("[BaseAppConfig.loadDiamond] Start to load diamond config to environment...");
        env.getPropertySources().addLast(new PropertiesPropertySource("diamond",
                MultiDiamondUtils.build(getDiamondConfigFile(), getDiamondBeanList()).getConfigProps()));
        logger.info("[BaseAppConfig.loadDiamond] Finish loading diamond config to environment.");
    }

    /**
     * 应用环境Diamond服务节点配置文件路径.
     * <br/>应用使用到Diamond时，子类必须要实现.
     * <br/>例如：/app/weblogic/.diamond.domain
     *
     * @return 应用环境主Diamond配置文件路径
     */
    protected String getDiamondConfigFile() {
        return MultiDiamondUtils.DEFAULT_CONFIG_FILE_PATH;
    }

    /**
     * 应用环境Diamond配置对象列表.
     *
     * @return Diamond配置对象列表
     */
    protected abstract List<DiamondBean> getDiamondBeanList();
}
